package com.ruoyi.sales.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sales.domain.ZcWorkshop;
import com.ruoyi.sales.service.IZcWorkshopService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车间信息Controller
 * 
 * @author woo
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/sales/workshop")
public class ZcWorkshopController extends BaseController
{
    @Autowired
    private IZcWorkshopService zcWorkshopService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询车间信息列表
     */
    @PreAuthorize("@ss.hasPermi('sales:workshop:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZcWorkshop zcWorkshop)
    {
        startPage();
        List<ZcWorkshop> list = zcWorkshopService.selectZcWorkshopList(zcWorkshop);
        return getDataTable(list);
    }

    /**
     * 导出车间信息列表
     */
    @PreAuthorize("@ss.hasPermi('sales:workshop:export')")
    @Log(title = "车间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZcWorkshop zcWorkshop)
    {
        List<ZcWorkshop> list = zcWorkshopService.selectZcWorkshopList(zcWorkshop);
        ExcelUtil<ZcWorkshop> util = new ExcelUtil<ZcWorkshop>(ZcWorkshop.class);
        util.exportExcel(response, list, "车间信息数据");
    }

    /**
     * 获取车间信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:workshop:query')")
    @GetMapping(value = "/{workshopId}")
    public AjaxResult getInfo(@PathVariable("workshopId") Long workshopId)
    {
        return success(zcWorkshopService.selectZcWorkshopByWorkshopId(workshopId));
    }

    /**
     * 新增车间信息
     */
    @PreAuthorize("@ss.hasPermi('sales:workshop:add')")
    @Log(title = "车间信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcWorkshop zcWorkshop)
    {
        String workshopLabel = sysDictDataService.selectDictLabel("zc_workshop", zcWorkshop.getName());
        String typeLabel = sysDictDataService.selectDictLabel("zc_workshop2", zcWorkshop.getType());
        if (zcWorkshopService.insertZcWorkshop(zcWorkshop)!=0){
            return AjaxResult.success(typeLabel+workshopLabel+"添加成功");
        }else {
            return AjaxResult.error(typeLabel+workshopLabel+"已存在，请重新选择需要新增的车间以及类型");
        }

    }

    /**
     * 修改车间信息
     */
    @PreAuthorize("@ss.hasPermi('sales:workshop:edit')")
    @Log(title = "车间信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcWorkshop zcWorkshop)
    {
        return toAjax(zcWorkshopService.updateZcWorkshop(zcWorkshop));
    }

    /**
     * 删除车间信息
     */
    @PreAuthorize("@ss.hasPermi('sales:workshop:remove')")
    @Log(title = "车间信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workshopIds}")
    public AjaxResult remove(@PathVariable Long[] workshopIds)
    {
        return toAjax(zcWorkshopService.deleteZcWorkshopByWorkshopIds(workshopIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('sales:workshop:edit')")
    @Log(title = "计划编码", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ZcWorkshop workshop)
    {
        return toAjax(zcWorkshopService.updateCodecStatus(workshop));
    }

//    @PreAuthorize("@ss.hasPermi('sales:workshop:check')")
//    @Log(title = "车间唯一", businessType = BusinessType.UPDATE)
//    @PutMapping("/check")
//    public AjaxResult checkWorkshop(@RequestBody ZcWorkshop workshop){
//        return toAjax(zcWorkshopService.checkWorkshopExists(workshop));
//    }
}
